﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;

namespace WikiCsharpCommon.Settings
{
    public partial class MySettingsControl : Common.Controls.SettingsControl
    {
        public override string this[string key]
        {
            get
            {
                if (!String.IsNullOrEmpty(key))
                {
                    switch (key.ToUpperInvariant())
                    {
                        case "FOLDER":
                            return WikiCsharpFolder;
                        case "FULLNAME":
                            return WikiCsharpFullname;
                    }
                }

                return null;
            }

            set
            {
                if (!String.IsNullOrEmpty(key))
                {
                    switch (key.ToUpperInvariant())
                    {
                        case "FOLDER":
                            WikiCsharpFolder = value;
                            break;
                        case "FULLNAME":
                            WikiCsharpFullname = value;
                            break;
                    }
                }
            }
        }

        public String WikiCsharpFolder
        {
            get { return txtWikiCsharpFolder.Text; }
            set { txtWikiCsharpFolder.Text = value; }
        }

        public String WikiCsharpFullname
        {
            get { return txtWikiCsharpFullname.Text; }
            set { txtWikiCsharpFullname.Text = value; }
        }

        public MySettingsControl()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if(fbd.ShowDialog() == DialogResult.OK)
            {
                txtWikiCsharpFolder.BackColor = Color.Empty;
                btnSetName.Enabled = false;
                txtWikiCsharpFullname.Enabled = false;

                WikiCsharpBugtracker wcbt = new WikiCsharpBugtracker();

                WikiCsharpFolder = fbd.SelectedPath;

                if (wcbt.ValidateBugTrackerFolder(WikiCsharpFolder))
                {
                    WikiCsharpFullname = wcbt.GetUserFullName(WikiCsharpFolder, Environment.UserName);

                    btnSetName.Enabled = true;
                    txtWikiCsharpFullname.Enabled = true;
                }
                else
                {
                    txtWikiCsharpFolder.BackColor = Color.LightPink;
                }
            }
        }

        private void btnSetName_Click(object sender, EventArgs e)
        {
            WikiCsharpBugtracker wcbt = new WikiCsharpBugtracker();

            if (!string.IsNullOrEmpty(WikiCsharpFullname) && wcbt.GetUserFullName(WikiCsharpFolder, Environment.UserName) != WikiCsharpFullname)
            {
                wcbt.AddUser(WikiCsharpFolder, Environment.UserName, WikiCsharpFullname);
            }
        }
        
        public void updateProgress(String step, int currentStep, int totalStep)
        {
            if (prgStatus.InvokeRequired)
            {
                prgStatus.Invoke(new ProgressCallback(updateProgress), step, currentStep, totalStep);
            }
            else
            {
                lblStatus.Text = step;
                prgStatus.Maximum = totalStep;
                prgStatus.Value = currentStep;
            }
        }
    }
}
